﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KMFrame.Permission.Util;
using LGCourt.Logic.BLL;
using LGCourt.Logic.NBL.DBEntities;
using System.IO;

namespace lgweb.lawyer
{
    public partial class evidence : System.Web.UI.Page
    {
        private string _AH = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!SessionBox.CheckUserSession())
            {
                Response.Redirect("/user/login.aspx");
            }
            GetParam();
            if (!this.IsPostBack)
            {
                GetCaseALLInfo();
            }
        }

        private void GetParam()
        {
            this._AH = string.IsNullOrEmpty(Request.QueryString["ah"]) ? "" : Request.QueryString["ah"];
            if (string.IsNullOrEmpty(this._AH))
            {
                Response.Redirect("/lawyer/index.aspx");
            }
            this.hdnAH.Value = this._AH;
            this.spAH.InnerHtml = this._AH;
        }

        /// <summary>
        /// 获取签收文书
        /// </summary>
        private void GetCaseALLInfo()
        {
            Lawyer bll = new Lawyer();

            int _userID = SessionBox.GetUserSession().UserID;
            UserCondition param = new UserCondition();
            param.AH = this._AH;
            param.UserID = _userID;

            CaseALLInfo result = (CaseALLInfo)bll.GetCaseALLInfo(param);

            if (result != null)
            {
                //当事人
                this.rptZJJHList.DataSource = result.ZJJHList;
                this.rptZJJHList.DataBind();

            }
        }

        //添加新证据
        protected void Button1_Click(object sender, EventArgs e)
        {
            CaseInfo bll = new CaseInfo();
            int _userID = SessionBox.GetUserSession().UserID;
            pZJJHB param = new pZJJHB();
            param.AH = this._AH;
            param.ZJMC = this.txtZJMC.Text;
            param.TJRXM = SessionBox.GetUserSession().UserName;

            doUpload();
            pPreCaseFile casefile = new pPreCaseFile();
            casefile.FileName = this.FileUpload1.FileName;
            string tempDir = "~//file//zhengjujiaohuan//";
            string urlPath = tempDir + this.FileUpload1.FileName;
            byte[] ib = new Byte[60000];
            FileStream fs = new FileStream(Server.MapPath(urlPath), FileMode.Open, FileAccess.Read);
            fs.Read(ib, 0, 60000);

            param.ZJNR = ib;
            bool result = bll.AddZJJH(param);

            if (result)
            {
                GetCaseALLInfo();
            }
        }


        protected void doUpload()
        {
            try
            {

                if (this.FileUpload1.HasFile)
                {
                    //if (this.FileUpload1.PostedFile.ContentLength > this.fileMaxSize)//file.FileSize > this.fileMaxSize
                    //{
                    //    throw new Exception("文件超过允许的大小！");
                    //}
                    string tempDir = "~//file//zhengjujiaohuan//";
                    string urlPath = tempDir + this.FileUpload1.FileName;

                    if (!Directory.Exists(Server.MapPath(tempDir)))
                    {
                        Directory.CreateDirectory(Server.MapPath(tempDir));
                    }
                    //上传文件并指定上传目录的路径   
                    this.FileUpload1.PostedFile.SaveAs(Server.MapPath(urlPath));

                }

            }
            catch (Exception ex)
            {
                //WriteJs("objUploadFile.uploaderror('" + ex.Message + "');");
            }
        }

    }
}